﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Bearinglib
{
    public partial class ModelSeletctWnd : Form
    {
        public ModelSeletctWnd()
        {
            InitializeComponent();

            p_bearinglib = new  Bearinglib.bearinglib();
			p_selbearing = null;
        }

        private void ModelSeletctWnd_Load(object sender, EventArgs e)
        {

            p_bearinglib.setsource("bearing");
		    p_bearinglib.Connect();

            //p_bearinglib.setsource("unittest");
            //p_bearinglib.Connect();

            int id1 = dataGridView1.Columns.Add("name", "名称");
            DataGridViewColumn acolumn = dataGridView1.Columns[id1];
           
            /*acolumn.Tag = pa;*/
            IBearingType tt = null;
            Ibearing[] li = p_bearinglib.FindBearings(paramname, paramvalue);
            if (li.Length > 0)  
            {
                
                Ibearing aa = li[0];
                tt = aa.ownertype;
                for (int i = 0; i < aa.ownertype.paramls.size; ++i)
                {
                    IParam pa = aa.ownertype.paramls[i];
                    int id2 = dataGridView1.Columns.Add(pa.name, pa.name);
                    DataGridViewColumn acolumn1 = dataGridView1.Columns[id2];
                    acolumn1.Tag = pa;

                }

            }
            foreach (Ibearing br in li)
            {

                String varString = br.name;
		        String varStringh = Convert.ToString(dataGridView1.Rows.Count + 1); 

		        int id =  dataGridView1.Rows.Add(varString);
		        DataGridViewRow row = dataGridView1.Rows[id];
		        row.HeaderCell.Value = varStringh;
		        row.Tag = br	;

		        foreach(DataGridViewCell cell in row.Cells)
		        {
			        DataGridViewColumn acolumn2 = dataGridView1.Columns[cell.ColumnIndex];
                    if (acolumn2.Tag != null)
			        {
                        IParam pp = (IParam)acolumn2.Tag;
				        IParamValue Val = br.GetValue(pp);
				        if(Val != null)
				        {
					        cell.Tag = Val;
					        cell.Value = Val.m_value;
				        }
			        }
		        }

                
                if (br.name == oldname)
                {
                    row.Selected = true;
                    dataGridView1.CurrentCell = row.Cells[0];
                }  

            }

               //dgv.Rows(index).Selected = True

            /*
            Dictionary<String,int> col_head = new Dictionary<String,int>();
            foreach (Ibearing br in li)
            {
                for(int i = 0 ; i < br.ownertype.paramls.size; ++i)
                {
                     IParam pa = br.ownertype.paramls[i];
                }
            }

            	foreach(param ^  pa in sortedparam)
	        {
		        String^ varString = gcnew String(pa->name);
		        if(pa->title != nullptr)
			        varString = pa->title;

		        int id = dataGridView1->Columns->Add(varString,varString);	
		        DataGridViewColumn^ acolumn = dataGridView1->Columns[id];
		        acolumn->Tag = pa;
	        }
              */
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (p_selbearing == null)
            {
                MessageBox.Show("请选择一个模型");
                return;
            }

            this.DialogResult = DialogResult.OK;

            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public Bearinglib.bearinglib p_bearinglib;
        public Bearinglib.Ibearing p_selbearing;

        public String paramname;
        public String paramvalue;

        public String oldname;

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
             if(dataGridView1.SelectedCells.Count > 0)
	        {	
		        DataGridViewCell aCell = dataGridView1.SelectedCells[0];
		        DataGridViewRow  ro = dataGridView1.Rows[aCell.RowIndex];
		        Bearinglib.Ibearing pb =  ro.Tag as  Bearinglib.Ibearing;
			  
		        if(pb != null)
		        {
			        System.Diagnostics.Debug.Assert(pb != null);	  
			        System.Diagnostics. Debug.Assert((bearing )pb != null);
			        p_selbearing = pb;
		        }
        			
	        } 
        }
    }
}
